//
//  AppDelegate.swift
//  weibo007
//
//  Created by 陈宗毅 on 15/12/15.
//  Copyright © 2015年 czy. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置导航栏外观
        setupAppearance()
        

        let userAccount = ZYUserAccount.loadUserAccount()
        print("userAccount:\(userAccount)")
        
        window = UIWindow()
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = ZYNewFeatureViewController()
        window?.makeKeyAndVisible()
        
        
        return true
    }
    /// 判断程序启动后显示的默认控制器
    private func defaultViewController() ->UIViewController {
        if !ZYUserAccount.userLogin{
            //用户没有登陆
            return ZYMainViewController()
        }
        //用户已经登陆
        return isNewVersion() ? ZYNewFeatureViewController():ZYWelcomeViewController()
    }
    // 目标控制器有2个
    // 谁来赋值切换AppDelegate,切换界面其实就是切换window的跟控制器.window是AppDelegate的一个属性,AppDelegate来负责切换最合适
    /// 切换程序的根控制器, isMain = true 表示切换到MainViewController, isMain = false 表示切换到WelcomeViewController
    
    class func outSwitchRootViewController(isMain:Bool) {

        (UIApplication.sharedApplication().delegate as! AppDelegate) .switchRootViewController(isMain)
        
        
    }
    
  private  func switchRootViewController(isMain:Bool)
    {
        window?.rootViewController = isMain ? ZYMainViewController():ZYWelcomeViewController()
    }
    ///设置导航栏的外观
    private func setupAppearance(){
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }
    //MARK:-判断是否是新版本
    private func isNewVersion() -> Bool
    {
        //获取到当前版本
        let currentVesion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]as! String
        print("当前版本：\(currentVesion)")
        //获得上一次保存的版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        print("沙盒版本：\(sandboxVersion)")
        
        let newVersion = currentVesion != sandboxVersion
        
        if newVersion {
            
            //保存当前版本
            NSUserDefaults.standardUserDefaults().setObject(currentVesion, forKey: sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
      
        //返回，当前版本大于上一次的版本
        return newVersion
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

